package com.canopys.study.open.opensource.sm;

import com.canopys.study.open.opensource.sm.domain.DemoDomain;
import com.canopys.study.open.opensource.sm.event.DemoEvent;
import com.canopys.study.open.opensource.sm.state.DemoState;
import lombok.extern.slf4j.Slf4j;
import org.squirrelframework.foundation.fsm.StateMachine;
import org.squirrelframework.foundation.fsm.annotation.OnActionExecException;
import org.squirrelframework.foundation.fsm.annotation.OnAfterActionExecuted;
import org.squirrelframework.foundation.fsm.annotation.OnBeforeActionExecuted;
import org.squirrelframework.foundation.fsm.annotation.OnTransitionBegin;
import org.squirrelframework.foundation.fsm.annotation.OnTransitionComplete;
import org.squirrelframework.foundation.fsm.annotation.OnTransitionDecline;
import org.squirrelframework.foundation.fsm.annotation.OnTransitionEnd;
import org.squirrelframework.foundation.fsm.annotation.OnTransitionException;
import org.squirrelframework.foundation.fsm.annotation.State;
import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;
import org.squirrelframework.foundation.fsm.annotation.States;
import org.squirrelframework.foundation.fsm.annotation.Transit;
import org.squirrelframework.foundation.fsm.annotation.Transitions;
import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine;

/**
 * @Author: Canopys
 * @Descrption:
 * @Date: Created in 11:24 2019/3/24
 * @Modified By:
 */
@Slf4j
@StateMachineParameters(stateType = DemoState.class, eventType = DemoEvent.class,
        contextType = DemoDomain.class)
@States({
        @State(name = "INIT", initialState = true),
        @State(name = "PROCESS"),
        @State(name = "FAIL"),
        @State(name = "SUCCESS"),
})
@Transitions({
        @Transit(from = "INIT", to = "PROCESS", on = "ORDER",
                callMethod = "doSomething"),
        @Transit(from = "PROCESS", to = "FAIL", on = "FAIL"),
        @Transit(from = "PROCESS", to = "SUCCESS", on = "SUCCESS")
})
public class DemoStateMachine extends AbstractUntypedStateMachine {

    public void doSomething(DemoState fromState, DemoState toState, DemoEvent event, DemoDomain domain) {
        log.info("-----------------------------doSomething--------------------------");
        int i = 1/0;
        //throw new RuntimeException("exception");
    }

    protected void beforeTransitionBegin(DemoState fromState,DemoEvent event, DemoDomain domain) {
        log.info("-----------------------------beforeTransitionBegin--------------------------");
        log.info("-----------------------------{}--------------------------", this.getCurrentState());
    }

    protected void beforeTransitionBegin(DemoState fromState, DemoState toState, DemoEvent event, DemoDomain domain) {
        log.info("-----------------------------beforeTransitionBegin--------------------------");
        log.info("-----------------------------{}--------------------------", this.getCurrentState());
    }

    protected void afterTransitionEnd(DemoState fromState, DemoState toState, DemoEvent event, DemoDomain domain) {
        log.info("-----------------------------afterTransitionEnd--------------------------");
        log.info("-----------------------------{}--------------------------", this.getCurrentState());
    }

    protected void afterTransitionDeclined(DemoState fromState, DemoState toState, DemoEvent event, DemoDomain domain) {
        log.info("-----------------------------afterTransitionDeclined--------------------------");
        log.info("-----------------------------{}--------------------------", this.getCurrentState());
    }

    protected void beforeActionInvoked(DemoState fromState, DemoState toState, DemoEvent event, DemoDomain domain) {
        log.info("-----------------------------beforeActionInvoked--------------------------");
        log.info("-----------------------------{}--------------------------", this.getCurrentState());
    }

    protected void afterActionInvoked(DemoState fromState, DemoState toState, DemoEvent event, DemoDomain domain) {
        log.info("-----------------------------beforeActionInvoked--------------------------");
        log.info("-----------------------------{}--------------------------", this.getCurrentState());
    }
}
